//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package org.pentaho.kettle.plugin.mqtt.ui.steps.consumer;

import org.eclipse.swt.widgets.Shell;
import org.pentaho.di.core.util.StringUtil;
import org.pentaho.di.i18n.BaseMessages;
import org.pentaho.di.trans.TransMeta;
import org.pentaho.di.trans.step.BaseStepMeta;
import org.pentaho.di.trans.step.StepDialogInterface;
import org.pentaho.di.ui.trans.step.BaseStepXulDialog;
import org.pentaho.kettle.plugin.mqtt.steps.consumer.ConsumerStepMeta;
import org.pentaho.ui.xul.binding.BindingConvertor;
import org.pentaho.ui.xul.binding.Binding.Type;
import org.pentaho.ui.xul.components.XulTextbox;

public class ConsumerStepDialog extends BaseStepXulDialog implements StepDialogInterface {
    private static final Class<?> PKG = ConsumerStepMeta.class;
    private String workingStepname;
    private String outputName;
    private ConsumerStepMetaMapper metaMapper;

    public ConsumerStepDialog(Shell parent, Object in, TransMeta tr, String sname) throws Throwable {
        super("org/pentaho/kettle/plugin/mqtt/ui/consumer/dialog.xul", parent, (BaseStepMeta)in, tr, sname);
        this.init();
    }

    public void init() throws Throwable {
        this.workingStepname = this.stepname;
        this.metaMapper = new ConsumerStepMetaMapper();
        this.metaMapper.loadMeta((ConsumerStepMeta)this.baseStepMeta);
        this.bf.setBindingType(Type.ONE_WAY);
        this.setTextBoxValue("output-field", this.metaMapper.getOutput());
        this.setTextBoxValue("hostname-field", this.metaMapper.getsBroker());
        this.setTextBoxValue("topic-field", this.metaMapper.getsTopic());
        this.setTextBoxValue("ClientID-field", this.metaMapper.getClientId());
        this.setTextBoxValue("qos-field", Integer.toString(this.metaMapper.getQos()));
        this.setTextBoxValue("username-field", this.metaMapper.getUsername());
        this.setTextBoxValue("password-field", this.metaMapper.getPassword());
        this.bf.createBinding("step-name", "value", this, "stepName", new BindingConvertor[0]);
        this.bf.createBinding(this, "stepName", "step-name", "value", new BindingConvertor[0]).fireSourceChanged();
    }

    private void setTextBoxValue(String textbox, String value) {
        ((XulTextbox)this.getXulDomContainer().getDocumentRoot().getElementById(textbox)).setValue(value);
    }

    protected Class<?> getClassForMessages() {
        return ConsumerStepMeta.class;
    }

    public void onAccept() {
        this.metaMapper.setOutput(this.fetchValue((XulTextbox)this.getXulDomContainer().getDocumentRoot().getElementById("output-field")));
        this.metaMapper.setsBroker(this.fetchValue((XulTextbox)this.getXulDomContainer().getDocumentRoot().getElementById("hostname-field")));
        this.metaMapper.setSTopic(this.fetchValue((XulTextbox)this.getXulDomContainer().getDocumentRoot().getElementById("topic-field")));
        this.metaMapper.setClientId(this.fetchValue((XulTextbox)this.getXulDomContainer().getDocumentRoot().getElementById("ClientID-field")));
        this.metaMapper.setQos(Integer.parseInt(this.fetchValue((XulTextbox)this.getXulDomContainer().getDocumentRoot().getElementById("qos-field"))));
        this.metaMapper.setUsername(this.fetchValue((XulTextbox)this.getXulDomContainer().getDocumentRoot().getElementById("username-field")));
        this.metaMapper.setPassword(this.fetchValue((XulTextbox)this.getXulDomContainer().getDocumentRoot().getElementById("password-field")));
        if (!this.workingStepname.equals(this.stepname)) {
            this.stepname = this.workingStepname;
            this.baseStepMeta.setChanged();
        }

        this.metaMapper.saveMeta((ConsumerStepMeta)this.baseStepMeta);
        this.dispose();
    }

    private String fetchValue(XulTextbox textbox) {
        String result = "";
        if (textbox != null && !StringUtil.isEmpty(textbox.getValue())) {
            try {
                result = textbox.getValue();
            } catch (NumberFormatException var4) {
                this.log.logError(BaseMessages.getString("HadoopEnter.Error.ParseInteger", textbox.getValue()));
            }
        }

        return result;
    }

    public void onCancel() {
        this.setStepName((String)null);
        this.dispose();
    }

    public void setStepName(String stepname) {
        this.workingStepname = stepname;
    }

    public String getStepName() {
        return this.workingStepname;
    }
}
